<?php

/*
    AArray test case
        Author:     Alfredo Mungo
        License:    LGPL
        Copyright:  2012
*/

define("TEST_TITLE", "AArray class");

include 'test_suit.php';
include '../core/core.php';
include '../modules/AArray.php';

function ah_test_read()
{
    $var = array(1, 2, 3, 4, 5, "sixth" => 6, 7, 8, 9, "last" => 10);
    $helper = new AArray($var);

    test_report_add_result("length()", $helper->length() == 10);
    test_report_add_result("keys()", $helper->keys()[5] == 'sixth' && $helper->keys()[9] == 'last');
    test_report_add_result("values()", $helper->values()[0] == 1 && $helper->values()['last'] == 10);
    test_report_add_result("get()", $helper->get('3') == 4 && $helper->get('sixth') == 6);
    test_report_add_result("hasKey()", $helper->hasKey('dummy') == false && $helper->hasKey('sixth'));
    test_report_add_result("first()", $helper->first() === 1);
    test_report_add_result("last()", $helper->last() === 10);
    
    
    $var = array(5, 4, 35);
    $helper = new AArray($var);
    $helper->sort();
    
    test_report_add_result("sort()", $helper->get(0) == 4 && $helper->get(1) == 5 && $helper->get(2) == 35);
    
}

function ah_test_write()
{
    $var = array(1, 2, 3, 4, 5, "sixth" => 6, 7, 8, 9, "last" => 10);
    $helper = new AArray($var);
    
    $helper->set(0, 666);
    test_report_add_result("set() - modify element", $helper->get(0) == 666);
    
    $helper->set('triple six', 666);
    test_report_add_result("set() - add element", $helper->get('triple six') == 666);
    
    $helper->remove('sixth');
    
    try {
        $helper->get('sixth');
        test_report_add_result("remove()", false);
    } catch (IndexOutOfBoundException $e) {
        test_report_add_result("remove()", true);
    }
    
    test_report_add_result("Data binding 1", $helper->get('triple six') == 666 && $helper->get(0) == 666 && $helper->get(1) == 2);
    
    $var = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    $helper = new AArray($var);
    $helper->push(777);
    test_report_add_result("push()", $helper->get(10) == 777);
    
    test_report_add_result("Data binding 2", $helper->get(0) == 1 && $helper->get(10) == 777);
    
    $popvar = $helper->pop();
    test_report_add_result("pop()", $popvar == 777 && $helper->length() - count($var) == 0);
    
    $rev = $helper->reverse();
    test_report_add_result("reverse()", $rev->get(0) == 10 && $rev->get(9) == 1);
    
    $helper->prepend(554);
    test_report_add_result("prepend()", $helper->first() === 554, $helper->get(1) == 1);
}

function ah_test_iteration() {
    $arr = array('first test key' => 200, 500, 'almost at the end' => 240, 777);
    $arr = new AArray($arr);
    $i = 0;
    
    foreach($arr as $k => $v)
        test_report_add_result("Value check #" . $i++, $v === $arr->get($k), "[$k] = $v");
}

test_register('ah_test_read', 'Reading test');
test_register('ah_test_write', 'Writing test');
test_register('ah_test_iteration', 'Iteration test');
test_run();

include 'test_template.php';

?>